package com.lgs.demo.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.lgs.demo.R;
import com.lgs.demo.base.BaseLazyFragment;
import com.lgs.demo.bean.PackData;
import com.lgs.demo.utils.LogUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lgs on 2018/1/28.
 * fragment第一次创建1~8
 * 熄屏后9~11
 * 亮屏后7~8
 * <p>
 * 切换到下一个Fragment时
 */

public class HomeFragment extends BaseLazyFragment {

    //    @BindView(R.id.gnp)
    //    GoodsNumberPicker mGoods;

    @BindView(R.id.tv_homeShow)
    TextView tvShow;


    public static HomeFragment getInstance() {  //1 构造器
        LogUtil.i("构造器");
        return new HomeFragment();
    }

    @Override
    public void onAttach(Context context) {//2 onAttach此方法只会执行一次
        super.onAttach(context);
        LogUtil.i("onAttach");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {//3 onCreate
        super.onCreate(savedInstanceState);
        LogUtil.i("onCreate");
        callback();
    }

    @Override
    protected int getLayoutId() {   //4 onCreateView
        return R.layout.fragment_home;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {//5 onViewCreated
        super.onViewCreated(view, savedInstanceState);
        LogUtil.i("onViewCreated");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {//6 onActivityCreated
        super.onActivityCreated(savedInstanceState);
        LogUtil.i("onActivityCreated");
    }

    @Override
    public void onStart() {//7 onStart
        super.onStart();
        Log.i("TAGAA", "首页界面 + onStart");
    }

    @Override
    public void onResume() {//8 onResume
        super.onResume();
        Log.i("TAGAA", "首页界面 + onResume");
    }

    @Override
    public void onPause() {//9 onPause
        super.onPause();
        LogUtil.i("onPause");
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {//10 onSaveInstanceState
        super.onSaveInstanceState(outState);
        LogUtil.i("onSaveInstanceState");
    }

    @Override
    public void onStop() {//11 onStop
        super.onStop();
        LogUtil.i("onStop");
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        LogUtil.i("setUserVisibleHint");
    }

    @Override
    public boolean getUserVisibleHint() {
        LogUtil.i("getUserVisibleHint");
        return super.getUserVisibleHint();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LogUtil.i("onDestroyView");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        LogUtil.i("onDetach");
    }

    @OnClick({R.id.btn_go_fragment, R.id.btn_go_activity, R.id.btn_dialog_show})
    public void onClickListener(View v) {
        MainActivity activity = (MainActivity) getActivity();
        switch (v.getId()) {

            case R.id.btn_go_activity:
                startActivity(new Intent(activity, NewActivity.class));
                break;

            case R.id.btn_dialog_show:
                //                callback();
                break;

            case R.id.btn_go_fragment:
                NextFragment nextFragment = NextFragment.newInstance();
                activity.pushFragment(this, nextFragment, R.id.fmContainer);
//                nextFragment.setFragment(this);
//                activity.pushFragment(R.id.fmContainer, nextFragment, true);

                break;
        }
    }

    //    private OnPassParams mPass;
    //
    //    public interface OnPassParams {
    //        void setData(PackData data);
    //    }
    //
    //    public void callback(OnPassParams pass) {
    //        this.mPass = pass;
    //    }
    //
    //
    private void callback() {
        //
        MainActivity activity = (MainActivity) getActivity();
        activity.callback(new MainActivity.OnPassParams() {
            @Override
            public void setData(PackData data) {
                Log.e("xxx", "setData: " + data.getName());
                tvShow.setText(data.disPshow());
            }
        });
    }


}
